package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.Checkout;
import com.qst.pojo.User;
import com.qst.pojo.Userlist;
import com.qst.service.CheckoutService;
import com.qst.service.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller @RequestMapping("/checkout") public class CheckoutController {
	 @Autowired private CheckoutService checkoutService;
	 @Autowired private UserlistService userlistService;

	 @RequestMapping("/getallcheckout") public String getallcheckout(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 HttpSession httpSession) {
		  User user = (User) httpSession.getAttribute("user");
		  List<Checkout> checkout = new ArrayList<Checkout>();
		  PageHelper.startPage(page, pageSize);

		  if ("admin".equals(user.getUsertype())) {
				checkout = checkoutService.getAllCheckout();
		  } else {
				checkout = checkoutService.getCheckout(user.getId());
		  }

		  PageInfo<Checkout> p = new PageInfo<Checkout>(checkout);
		  model.addAttribute("p", p);
		  model.addAttribute("checkout", checkout);
		  model.addAttribute("mainPage", "../checkout/checkout.jsp");
		  return "common/main";
	 }

	 // 租客删除自己已退租列表
	 @RequestMapping("/deletecheckout") public String deletecheckout(Integer id) {
		  checkoutService.deletecheckout(id);
		  return "redirect:/checkout/getmycheckout.action";
	 }

	 // 租客删除自己已退租列表
	 @RequestMapping("/admindeletecheckout") public String admindeletecheckout(Integer id) {
		  checkoutService.deletecheckout(id);
		  return "redirect:/checkout/getallcheckout.action";
	 }

	 @RequestMapping("/getmycheckout") public String getmycheckout(Model model,
			 HttpSession httpSession, @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		  User user1 = (User) httpSession.getAttribute("user");
		  Userlist userlist = userlistService.findhasuserlist(user1.getId());
		  PageHelper.startPage(page, pageSize);
		  List<Userlist> list = userlistService.getmycheckout(userlist.getId());
		  PageInfo<Userlist> p = new PageInfo<Userlist>(list);
		  model.addAttribute("p", p);
		  model.addAttribute("userlistcheck", list);
		  model.addAttribute("mainPage", "../checkout/mycheckout.jsp");
		  return "common/main";
	 }
}
